package org.rayken.trix.commons.deck.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.rayken.trix.commons.card.Card;
import org.rayken.trix.commons.deck.Deck;

public class DeckImpl implements Deck {

    private LinkedList<Card> cards = new LinkedList<Card>();

    public DeckImpl() {
		for (Card card : Card.values()) {
		    cards.add(card);
		}
    }
		
	public Card extract() {
		if (cards.isEmpty())
			throw new IllegalStateException("there are no cards");
		return cards.poll();
	}

	public List<Card> extract(int howMany) {
		try {
			List<Card> extracted = new ArrayList<Card>(cards.subList(0, howMany));
			cards.removeAll(extracted);
			return extracted;
		} catch (Exception ex){
			throw new IllegalStateException("there are fewer than " + howMany + " cards left in the deck");
		}
	}
	
	public void shuffle() {
		Collections.shuffle(cards);
	}

}
